package com.itheima.goods.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.goods.pojo.Spec;
import com.itheima.goods.service.SpecService;
import entity.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("spec")
public class SpecController {

    @Autowired
    SpecService specService;

    @GetMapping
    public List<Spec> findAll() {
        return specService.list();
    }

    @PostMapping
    public Result<Spec> add(@RequestBody Spec spec){
        specService.save(spec);
        return new Result<>(true,20000,"查询成功");
    }

    @GetMapping("search/{page}/{size}")
    public Result<Spec> findPage(@PathVariable("page")Integer page,
                                 @PathVariable("size")Integer size){
        Page<Spec> page1 = new Page<>(page, size);
        QueryWrapper<Spec> wrapper = new QueryWrapper<>();
        wrapper.ge("id",2);
        specService.getById(wrapper);
        return new Result<>(true,20000,"查询成功");
    }

    @GetMapping("/{id}")
    public Result<Spec> findById(@PathVariable("id")Integer id){
        specService.getById(id);
        return new Result<>(true,20000,"查询成功");
    }

    @PutMapping("/{id}")
    public Result<Spec> update(@RequestBody Spec spec,@PathVariable("id")Integer id){
        LambdaQueryWrapper<Spec> wrapper = Wrappers.<Spec>lambdaQuery();
        wrapper.eq(Spec::getId,spec.getId());
        specService.update(spec,wrapper);
        return new Result<>(true,20000,"查询成功");
    }

    @DeleteMapping("/{id}")
    public Result<Spec> delete(@PathVariable("id")Integer id){
        specService.removeById(id);
        return new Result<>(true,20000,"查询成功");
    }
}
